% GE_event_recoding: recodes events for iteratively training on half or third of the data; alternatively to limit number of neighbors for reversed lexical and all neighbors
function GE_event_recoding(iteration, condition_tag)

%Initialize variables
svm_dir = '/autofs/space/clive_001/users/adriana/GE_SVM/';
SubjectNames = {'GE_05', 'GE_06', 'GE_07', 'GE_08', 'GE_09', 'GE_10', 'GE_11', 'GE_12', 'GE_13', 'GE_14', 'GE_15', 'GE_16'};
roisets = {'newSMG', 'newpMTG', 'TempPole', 'FrontalPole', 'CentGyri'};

for i_subject=1:length(SubjectNames)
    subject = SubjectNames{i_subject};
    
    roi = roisets{1};
    %Load the lvn_all condition index
    epochfile = sprintf('%s/%s/epochs/%s_%s_tokens_all.mat', svm_dir, subject, subject, roi);
    load(epochfile, 'condition_index');

    conditions = cellstr(unique(condition_index));

    %Randomly assign condititions
    for i_cond = 1:length(conditions)
        neighborhood = conditions{i_cond}(end);
        indices = find(condition_index == str2num(conditions{i_cond}));
	if conditions{i_cond}(1) == '1'
            condition_index(indices) = str2num(neighborhood);
        else
            switch condition_tag
                case 'nvl'
                    permuter = randperm(length(indices), 11);
                    permuted_indices = indices(permuter);
		    if conditions{i_cond}(1) == '2'
                        condition_index(permuted_indices) = str2num([neighborhood '0']);
		    end
                    if conditions{i_cond}(1) == '3'
                        condition_index(permuted_index) = str2num([neighborhood '00']);
                    end
                case 'svn'
                   permuter = randperm(length(indices), 6);
                   permuted_indices = indices(permuter);
                   condition_index(permuted_indices) = str2num([neighborhood neighborhood]);
                case 'half'
                   permuter = randperm(length(indices));
                   permuted_indices = indices(permuter);
                   division = round(length(indices)/2);
                   condition_index(permuted_indices(1:division)) = str2num([neighborhood '01']);
                   condition_index(permuted_indices(division+1:end)) = str2num([neighborhood '02']);
                case 'third'
                   permuter = randperm(length(indices));
                   permuted_indices = indices(permuter);
                   division = round(length(indices)/3);
                   condition_index(permuted_indices(1:division)) = str2num([neighborhood '001']);
                   condition_index(permuted_indices(division+1:division+division)) = str2num([neighborhood '002']);
                   condition_index(permuted_indices(division+division+1:end)) = str2num([neighborhood '003']);
            end
        end
    end

    if condition_tag == 'half' | condition_tag == 'third'
        %Save the condition_index (separate from epochs--load later)
    	recoded_dir = sprintf('%s/%s/epochs/recoded_events', svmdir, subject);
    
        if iteration == 1
            mkdir(recoded_dir)
        end
    
       for i_roiset = 1:length(roisets)
            roi=roisets{i_roiset};
            recoded_file = sprintf('%s/%s_%s_%s%d_all.mat', recoded_dir, subject, roi, condition_tag, iteration);
            save(recoded_file, 'condition_index');
        end %i_roi
    else
        for i_roiset = 1:length(roisets)
            roi = roisets{i_roiset};
            epochfile = sprintf('%s/%s/epochs/%s_%s_tokens_all.mat', svm_dir, subject, subject, roi);
            load(epochfile, 'epoch_data', 'times'); %check to see what all the variables are named
            recoded_file = sprintf('%s/%s/epochs/%s_%s_%s_all.mat', svm_dir, subject, subject, roi, condition_tag);
            save(recoded_file, 'epoch_data', 'condition_index', 'times'); %check to see what all the variables are/what order
        end %i_roi
    end

end %i_subject

end %function
